

package org.apache.ocean;

import java.util.logging.*;
import java.io.*;
import java.text.*;
import java.util.Date;
import java.sql.SQLException;
import java.util.concurrent.locks.*;
import org.apache.commons.lang.exception.*;
import org.apache.commons.dbutils.DbUtils;

/**
 * Prints out full stack traces of all nested exceptions
 *
 * @author jasonr
 */
public class ClusterLogFormatter extends java.util.logging.Formatter {
  Date dat = new Date();
  private final static String format = "{0,date} {0,time}";
  private MessageFormat formatter;
  private ReentrantLock lock = new ReentrantLock();
  
  private Object args[] = new Object[1];
  
  public static void main(String[] args) {
    LogManager logManager = LogManager.getLogManager();
    //logManager.
  }
  
  // Line separator string.  This is the value of the line.separator
  // property at the moment that the SimpleFormatter was created.
  static private String lineSeparator = null;//(String) java.security.AccessController.doPrivileged(
          //new sun.security.action.GetPropertyAction("line.separator"));
  static {
    lineSeparator = System.getProperty("line.separator");
  }
  
  
  public ClusterLogFormatter() {
    super();
  }
  
  public String format(LogRecord record) {
    lock.lock();
    try {
      StringBuffer sb = new StringBuffer();
      // Minimize memory allocations here.
      dat.setTime(record.getMillis());
      args[0] = dat;
      StringBuffer text = new StringBuffer();
      if (formatter == null) {
        formatter = new MessageFormat(format);
      }
      formatter.format(args, text, null);
      sb.append(text);
      sb.append(" ");
      if (record.getSourceClassName() != null) {
        sb.append(record.getSourceClassName());
      } else {
        sb.append(record.getLoggerName());
      }
      if (record.getSourceMethodName() != null) {
        sb.append(" ");
        sb.append(record.getSourceMethodName());
      }
      sb.append(lineSeparator);
      String message = formatMessage(record);
      sb.append(record.getLevel().getLocalizedName());
      sb.append(": ");
      sb.append(message);
      sb.append(lineSeparator);
      if (record.getThrown() != null) {
        Throwable throwable = record.getThrown();
        if (throwable instanceof SQLException) {
          SQLException sqlException = (SQLException)throwable;
          StringWriter stringWriter = new StringWriter();
          DbUtils.printStackTrace(sqlException, new PrintWriter(stringWriter));
          sb.append(stringWriter.toString());
        } else {
          String string = ExceptionUtils.getFullStackTrace(throwable);
          sb.append(string);
        }
        /**
         * try {
         * StringWriter sw = new StringWriter();
         * PrintWriter pw = new PrintWriter(sw);
         * record.getThrown().printStackTrace(pw);
         * pw.close();
         * sb.append(sw.toString());
         * } catch (Exception ex) {
         * }
         **/
      }
      return sb.toString();
    } finally {
      lock.unlock();
    }
  }
}
